//
//  ShellLuanchController.m
//  AppShellTool
//
//  Created by IOS01 on 2022/12/2.
//

#import "ShellLuanchController.h"
#import "ShellLuanchTool.h"
#import "BundleTool.h"
#import "ParamsTool.h"
#import "UINavigationController+mxswizzling.h"
#import "UIViewController+MxSwizzling.h"

@interface ShellLuanchController ()

@property(nonatomic, strong) NSMutableArray <UIViewController *> *myChildControllers;

@end

@implementation ShellLuanchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [UIApplication sharedApplication].delegate.window;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navi_return"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClick:)];
    self.title = @"外壳弹来";
    [self initUI];
}

- (NSMutableArray<UIViewController *> *)myChildControllers{
    if (_myChildControllers == nil) {
        _myChildControllers = [[NSMutableArray alloc] init];
    }
    return _myChildControllers;
}

- (void)initUI{
    
    [self addChildController];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}

- (void)addChildController{
    
    for (UIViewController *vc in self.myChildControllers) {
        [vc removeFromParentViewController];
        [vc.view removeFromSuperview];
    }
    
    NSDictionary *dict = [BundleTool loadBundleJson];
    
    if ([dict isKindOfClass:NSDictionary.class]) {
        
        NSString *url = [ShellLuanchTool shareTool].url;
        NSString *param = [ParamsTool getParamByName:@"param" URLString:url];
        if ([ShellLuanchTool shareTool].giveControllerBlock) {
            UIViewController *vc = [ShellLuanchTool shareTool].giveControllerBlock(self,param);
            if (vc) {
                vc.view.frame = self.view.bounds;
                [self.view addSubview:vc.view];
                [self addChildViewController:vc];
                [self.myChildControllers addObject:vc];
                return;
            }
        }
        
        NSDictionary *routerDict = [dict objectForKey:@"router"];
        if (!routerDict) {
            NSLog(@"路由配置错误");
            return;
        
        }
        NSString *router = [ParamsTool getParamByName:@"router" URLString:url];

        
        NSString *className = [routerDict objectForKey:router];
        if (className) {
            Class class = NSClassFromString(className);
            
            UIViewController *ctroller = [[class alloc] init];
            if (ctroller) {
                ctroller.view.frame = self.view.bounds;
                [self.view addSubview:ctroller.view];
                [self addChildViewController:ctroller];
                [self.myChildControllers addObject:ctroller];
            }else{
                NSLog(@"该类未创建");
            }
        }else{
            NSLog(@"路由未配置");
        }
    }
    
}

- (void)backBtnClick:(UIBarButtonItem *)item{
    [[ShellLuanchTool shareTool] shellIsBack];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
